//
//  FagetPassWordViewController.m
//  17DianYing
//
//  Created by apple on 15/9/12.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "FagetPassWordViewController.h"
#import "UserLondManager.h"
#import "MUser.h"


@interface FagetPassWordViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneID;
@property (weak, nonatomic) IBOutlet UITextField *anewpassword;
@property(nonatomic,strong)UserLondManager *afManager;
@property (weak, nonatomic) IBOutlet UIButton *eyebuton;

@property (weak, nonatomic) IBOutlet UIButton *seedIdentifyingButton;

@property (weak, nonatomic) IBOutlet UITextField *code;
@property(nonatomic,assign)int a;
@end

@implementation FagetPassWordViewController
{
    NSTimer * timer;
    int count;
    NSString * code1;
}
- (IBAction)topCodeButton:(id)sender
{
    if ([self isRightTEL:self.phoneID.text])
    {
//        发送信息
        timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(jishi11) userInfo:nil repeats:YES];
        count=30;
        
        
        [self.afManager forgetWithPhonID:self.phoneID.text BackBlock:^(NSDictionary *dic)
         {
             NSString *str = [NSString stringWithFormat:@"%@",dic[@"status"]];
             NSLog(@"%@",dic);
             if ([str isEqualToString:@"0"])
             {
                 [MUser alertViewShowTitle:@"提示" andMessage:@"验证码将会发送到您的手机上，\n请注意查收" andDelegate:self controltag:1000];
             }
             
             else
             {
                 [MUser alertViewShowTitle:@"提示" andMessage:@"验证码获取失败，请重试" andDelegate:self controltag:1000];
             }
             
         }];
    }
    else
    {
        [MUser alertViewShowTitle:@"提示" andMessage:@"请输入正确手机号" andDelegate:self controltag:2];
    }

}
- (IBAction)topConfirmButton:(id)sender
{
    
    if (![self.code.text isEqualToString:@""])
    {
        if ([self isRightTEL:self.phoneID.text])
        {
            if (![self.anewpassword.text isEqualToString:@""])
            {

            [self.afManager fagetaWithPhonID:self.phoneID.text andNewPassWord:self.anewpassword.text andCode:self.code.text BlockHandle:^(NSDictionary *dic) {
                NSLog(@"%@",dic[@"status"]);
                NSString *str = [NSString stringWithFormat:@"%@",dic[@"status"]];
                
                
                if ([str isEqualToString:@"0"])
                {
                    UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"提示" message:@"修改成功" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                    [a show];
                    
                    
                    UIStoryboard *s = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
                    UIViewController *vc = [s instantiateViewControllerWithIdentifier:@"UserLond"];
                    [self presentViewController:vc animated:YES completion:nil];
                }
                else
                {
                    UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"提示" message:@"重置失败" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                    [a show];
                }
                
            }];
            }
            else
            {
                [MUser alertViewShowTitle:@"提示" andMessage:@"请输入密码" andDelegate:self controltag:1000];
            }
        }
        else
        {
            [MUser alertViewShowTitle:@"提示" andMessage:@"请输入正确手机号" andDelegate:self controltag:2];
        }

    }
   else
   {
       [MUser alertViewShowTitle:@"提示" andMessage:@"请输入验证码" andDelegate:self controltag:2];
   }
    
    
}
-(BOOL)isRightTEL:(NSString *)tel
{
    if (tel.length==11) {
        return YES;
    }
    return NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.phoneID becomeFirstResponder];
    self.seedIdentifyingButton.layer.cornerRadius  = 8;
    
    self.seedIdentifyingButton.layer.masksToBounds = YES;
    self.a = 1;
    self.anewpassword.secureTextEntry = YES;
    self.afManager = [[UserLondManager alloc]init];
    // Do any additional setup after loading the view.
    
    UITapGestureRecognizer *a = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(a:)];
    [self.view addGestureRecognizer:a];
    
}

-(void)a:(UITapGestureRecognizer*)sender
{
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    [self.phoneID becomeFirstResponder];
    // Dispose of any resources that can be recreated.
}
-(void)jishi11
{
    if (count==0)
    {
        [timer invalidate];
        self.seedIdentifyingButton.userInteractionEnabled=YES;
        self.seedIdentifyingButton.backgroundColor=[UIColor whiteColor];
        [self.seedIdentifyingButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
    else{
        count--;
        self.seedIdentifyingButton.userInteractionEnabled=NO;
        self.seedIdentifyingButton.backgroundColor=[UIColor grayColor];
        [self.seedIdentifyingButton setTitle:[NSString stringWithFormat:@"重新获取(%d)",count] forState:UIControlStateNormal];
    }
}
- (IBAction)tupEyesButton:(id)sender
{
    if (self.a == 1)
    {
        self.anewpassword.secureTextEntry=NO;
        self.a--;
    }
    else
    {
        self.anewpassword.secureTextEntry=YES;
        self.a++;
    }

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [self.view endEditing:YES];
    
}

@end
